#include "xymodemfile.h"

XymodemFile::XymodemFile(const QString& file_path) : m_seek(0), m_block_size(128)
{
	m_fileHandler = new QFile(file_path);
	m_fileHandler->open(QIODevice::ReadOnly);
}

int XymodemFile::size() const
{
	if (m_fileHandler == nullptr)
	{
		return -1;
	}
	else
	{
		return m_fileHandler->size();
	}
}

QString XymodemFile::filename() const
{
	if (m_fileHandler == nullptr)
	{
		return nullptr;
	}
	else
	{
		return m_fileHandler->fileName();
	}
}

QByteArray XymodemFile::get_block(const int block)
{
	m_seek = block * m_block_size;
	m_fileHandler->seek(m_seek);
	QByteArray body = m_fileHandler->read(m_block_size);
	// 如果不够
	const int getSize = body.size();
	if (getSize != m_block_size)
	{
		body.append(m_block_size - getSize, '\0');
	}
	return body;
}

void XymodemFile::set_block_size(const int size)
{
	m_block_size = size;
}

XymodemFile::~XymodemFile()
= default;
